<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Agent;

use App\Dao\AgentBankDao;
use App\Dao\AgentDao;
use App\Dao\AgentMoneyDao;
use App\Dao\BusinessDao;
use App\Dao\LocationDao;
use App\Dao\UserDao;
use App\Kernel\Util\AgentJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 区域服务
 *
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class RegionService extends Service
{

    /**
     * @Inject
     * @var LocationDao
     */
    public $location_dao;

    /**
     * @Inject
     * @var AgentDao
     */
    public $agent_dao;

    /**
     * @Inject
     * @var MoneyService
     */
    public $money_service;

    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * 区域服务
     */
    public function list()
    {
        $agent = AgentJwtInstance::instance()->build()->getAgent();

        if( $agent['identity'] != 2 ) {
            $this->error("COMMON.HANDLE_FAILED");
        }

        $city = $this->location_dao->getLocationByPid( [
            'pid' => $agent['location_id']
        ] );

        $district = $this->location_dao->getLocationByPid( [
            'pid' => $city['level']
        ] );

        $map = [
            'level' => $agent['location_id']
        ];

        $result = $this->location_dao->getLocationsByMap($map);

        $data = [];
        if( $result ) {
            foreach ($result as $key => $value) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['name'] = $value['name'];
                $agent = $this->agent_dao->map([
                    'location_id' => $value['pid']
                ],false);
                if($agent) {
                    $data['list'][$key]['type'] = 1;
                    $data['list'][$key]['agent_name'] = $agent['name'] ?? '';
                    $data['list'][$key]['created_at'] = date('Y.m.d',$agent['created_at']);
                    $data['list'][$key]['phone'] = $agent['phone'];
                    $data['list'][$key]['address'] = $agent['address'];
                    $company = $this->agent_dao->map([
                        'id' => $value['inviter_id']
                    ],false);
                    $data['list'][$key]['company_name'] = $company['name'] ?? '';
                    $business_where = [
                        'district_id' => $agent['location_id'],
                        'status' => 1,
                    ];
                    $channel_where = [
                        'district_id' => $agent['location_id'],
                        'group_id' => 2,
                    ];
                    $business_count = $this->business_dao->count($business_where);
                    $channel_count = $this->user_dao->count($channel_where);

                    $data['list'][$key]['business_count'] = $business_count ?? 0;
                    $data['list'][$key]['channel_count'] = $channel_count  ?? 0;

                    $data['list'][$key]['business_money'] = $this->money_service->getMoneyBalanceByStore($agent['id']);
                    $data['list'][$key]['channel_money'] = $this->money_service->getMoneyBalanceByCommission($agent['id']);
                    $data['list'][$key]['total_money'] = $this->money_service->getMoneyBalanceByAgent($agent['id']);
                } else {
                    $data['list'][$key]['type'] = 0;
                }

            }
        }

        $data['address'] = $district['name'].$city['name'];

        return $data;
    }

    /**
     * 分公司直招区县代理
     */
    public function directMoveArea()
    {
        $agent = AgentJwtInstance::instance()->build()->getAgent();

        if( $agent['identity'] == 1 || $agent['identity'] == 2 ) {
            $this->error("COMMON.HANDLE_FAILED");
        }

        $partner = $this->agent_dao->mapMany([
            'inviter_id' => $agent['id'],
            'identity' => 1,
        ],false);

        $data = [];
        if( $partner ) {
            foreach ( $partner as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['name'] = $value['name'];
                $location = $this->location_dao->getLocationByPid([
                    'pid' => $value['location_id']
                ]);
                $data['list'][$key]['location_name'] = $location['name'];
                $data['list'][$key]['created_at'] = date('Y.m.d',$agent['created_at']);
                $data['list'][$key]['phone'] = $agent['phone'];
                $data['list'][$key]['address'] = $agent['address'];

                $business_where = [
                    'district_id' => $value['location_id'],
                    'status' => 1,
                ];

                $channel_where = [
                    'district_id' => $value['location_id'],
                    'group_id' => 2,
                ];
                $channel_count = $this->user_dao->count($channel_where);
                $business_count = $this->business_dao->count($business_where);

                $data['list'][$key]['business_count'] = $business_count ?? 0;
                $data['list'][$key]['channel_count'] = $channel_count  ?? 0;

                $cityNum = $this->money_service->agent_money_dao->sumMoneyBalance([
                    'agent_id' => agentId(),
                    'area_id' => $value['id'],
                    'type' => 1,
                ]);
                $data['list'][$key]['total_money'] = filterMoney($cityNum,4);

                $storeNum = $this->money_service->agent_money_dao->sumMoneyBalance([
                    'agent_id' => agentId(),
                    'source' => 1,
                    'price_source' => 1,
                    'type' => 1,
                ]);
                $data['list'][$key]['business_money'] = filterMoney($storeNum,4);

                $channelNum = $this->money_service->agent_money_dao->sumMoneyBalance([
                    'agent_id' => agentId(),
                    'source' => 1,
                    'price_source' => 2,
                    'type' => 1,
                ]);
                $data['list'][$key]['channel_money'] = filterMoney($channelNum,4);
            }
        }

        $count = $this->agent_dao->count([
            'inviter_id' => $agent['id'],
            'identity' => 1,
        ]);

        $data['count'] = $count ?? 0;
        return $data;
    }

    /**
     *  分公司直招市代理
     */
    public function directMoveCity()
    {
        $agent = AgentJwtInstance::instance()->build()->getAgent();

        if( $agent['identity'] == 1 || $agent['identity'] == 2 ) {
            $this->error("COMMON.HANDLE_FAILED");
        }

        $partner = $this->agent_dao->mapMany([
            'inviter_id' => $agent['id'],
            'identity' => 2,
        ],false);

        $data = [];
        if( $partner ) {
            foreach ( $partner as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['name'] = $value['name'];
                $location = $this->location_dao->getLocationByPid([
                    'pid' => $value['location_id']
                ]);
                $data['list'][$key]['location_name'] = $location['name'];
                $data['list'][$key]['created_at'] = date('Y.m.d',$agent['created_at']);
                $data['list'][$key]['phone'] = $agent['phone'];
                $data['list'][$key]['address'] = $agent['address'];

                $business_where = [
                    'city_id' => $value['location_id'],
                    'status' => 1,
                ];

                $business_count = $this->business_dao->count($business_where);
                $data['list'][$key]['business_count'] = $business_count ?? 0;

                $partnerNum = $this->money_service->agent_money_dao->sumMoneyBalance([
                    'agent_id' => agentId(),
                    'city_id' => $value['id'],
                    'type' => 1,
                ]);

                $data['list'][$key]['total_money'] = filterMoney($partnerNum,4) ?? 0;
            }
        }

        $count = $this->agent_dao->count([
            'inviter_id' => $agent['id'],
            'identity' => 2,
        ]);
        $data['count'] = $count ?? 0;
        return $data;
    }
}